package com.chushouya.order.dao.repository;

import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.StatManageDayEntity;
import com.chushouya.order.dao.mapper.StatManageDayEntityMapper;
import com.chushouya.order.dto.admin.stat.ManageDataDTO;
import com.chushouya.order.dto.admin.stat.ManageDayStatQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Repository
public class StatManageDayRepository extends BaseRepository<StatManageDayEntityMapper, StatManageDayEntity, Long> {

    public StatManageDayEntity selectOne(ManageDayStatQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public List<StatManageDayEntity> selectList(ManageDayStatQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    private Example buildExample(ManageDayStatQuery query) {
        final Example example = this.buildExample();
        if (Objects.nonNull(query.getStatDate())) {
            example.and().andEqualTo("statDate", query.getStatDate());
        }
        if (Objects.nonNull(query.getStatDateStart()) && Objects.nonNull(query.getStatDateEnd())) {
            example.and().andGreaterThanOrEqualTo("statDate", query.getStatDateStart());
            example.and().andLessThanOrEqualTo("statDate", query.getStatDateEnd());
        }
        example.orderBy("statDate").desc();
        return example;
    }

    public Integer selectVisitCount(Date startTime, Date endTime) {
        return this.mapper.selectVisitCount(startTime, endTime);
    }

    public Integer selectOrderCount(Date startTime, Date endTime) {
        return this.mapper.selectOrderCount(startTime, endTime);
    }

    public Integer selectUserCount(Date startTime, Date endTime) {
        return this.mapper.selectUserCount(startTime, endTime);
    }

    public Integer selectEvaluateCount(Date startTime, Date endTime) {
        return this.mapper.selectEvaluateCount(startTime, endTime);
    }

    public Integer selectProductCount() {
        return this.mapper.selectProductCount();
    }

    public ManageDataDTO selectManageCount(Date startTime, Date endTime) {
        return this.mapper.selectManageCount(startTime, endTime);
    }
}
